﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

using Bricks.Business.Data.Initialize;
using Bricks.Business.Service.Helper.Ioc;
using System.Configuration;
using Bricks.Component.Utils;


namespace Bricks.Site.Web
{
    // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明，
    // 请访问 http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            //设置MEF依赖注入容器
            DirectoryCatalog catalog = new DirectoryCatalog(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath);
            MefDependencySolver solver = new MefDependencySolver(catalog);
            DependencyResolver.SetResolver(solver);

            bool migrationEnabled = ConfigurationManager.AppSettings["MigrationEnabled"].CastTo(true);  //读取配置文件节点，判断是否开启数据迁移
            if (migrationEnabled.Equals(true))
            {
                DatabaseInitializer.Initialize();
            }
        }

        /// <summary>
        /// 全局的异常处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Application_Error(object sender, EventArgs e)
        {
            //string s = HttpContext.Current.Request.Url.ToString();
            //HttpServerUtility server = HttpContext.Current.Server;
            //if (server.GetLastError() != null)
            //{
            //    Exception lastError = server.GetLastError();
            //    // 此处进行异常记录，可以记录到数据库或文本，也可以使用其他日志记录组件。
            //    //ExceptionHander.WriteException(lastError);  //Luozx：后续我们考虑将异常保存到LogInfoes表或者文本文件
            //    Application["LastError"] = lastError;
            //    int statusCode = HttpContext.Current.Response.StatusCode;
            //    string exceptionOperator = "/SysException/Error";  //异常的具体显示页面（后续处理）
            //    try
            //    {
            //        if (!String.IsNullOrEmpty(exceptionOperator))
            //        {
            //            exceptionOperator = new System.Web.UI.Control().ResolveUrl(exceptionOperator);
            //            string url = string.Format("{0}?ErrorUrl={1}", exceptionOperator, server.UrlEncode(s));
            //            string script = String.Format("<script language='javascript' type='text/javascript'>window.top.location='{0}';</script>", url);
            //            Response.Write(script);
            //            Response.End();
            //        }
            //    }
            //    catch { }
            //}
        }
    }
}